/*
	Naquadah Generator for GarrysMod10, based on aVoN's ZPM
	Copyright (C) 2007 RononDex

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
--################# HEADER #################
AddCSLuaFile("cl_init.lua");
AddCSLuaFile("shared.lua");
include("shared.lua");
--################# SENT CODE ###############

--################# Init 
function ENT:Initialize()
	self.Entity:SetModel("models/naquada-reactor.mdl");
	self.Entity:SetMaterial("materials/models/reactor-skin-off");
	self.Entity:PhysicsInit(SOLID_VPHYSICS);
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS);
	self.Entity:SetSolid(SOLID_VPHYSICS);
	self.Entity:SetMaxHealth(100)
 	self.Entity:SetHealth(100)
	self.MaxEnergy = StarGate.CFG:Get("Naquadah_Generator","capacity",5000000); 
	self.enabled = false;
	self:AddResource("Naquadah",self.MaxEnergy);
	self:SupplyResource("Naquadah",self.MaxEnergy);
	self:AddResource("energy",StarGate.CFG:Get("Naquadah_Generator","energy_capacity",10000)); 
	self:CreateWireOutputs("Active","Energy","Naquadah");
	local phys = self.Entity:GetPhysicsObject();
	if(phys:IsValid()) then
		phys:Wake();
		phys:SetMass(10);
	end

end


--############### Makes it spawn
function ENT:SpawnFunction(pl, tr)
	if (!tr.HitWorld) then return end;
	local e = ents.Create("naquadah_generator");
	e:SetPos(tr.HitPos + Vector(0, 0, 50));
	e:Spawn();
	return e;
end

--################# Adds the overlay speechbubble
function ENT:ShowOutput(v,force)
	local add = "(Off)";
	if(self.enabled) then add = "(On)" end;
	self:SetOverlayText("Naquadah Generator "..add.."\n"..v.."%");
end

--################# Think 
function ENT:Think()
	if(self.depleted or not self.HasResourceDistribution) then return end;
	local energy = self:GetResource("energy");
	local Naquadah = self:GetResource("Naquadah");
	local my_capacity = self:GetUnitCapacity("energy");
	local nw_capacity = self:GetNetworkCapacity("energy");
	local percent = (Naquadah/self.MaxEnergy)*100;
	if(my_capacity ~= nw_capacity) then
		if(not self.enabled) then
			self.Entity:SetMaterial("materials/models/reactor-skin");
			self.enabled = true;
		end
	else
		if(self.enabled) then
			self.Entity:SetMaterial("materials/models/reactor-skin-off");
			self.enabled = false;
		end
	end
	-- No Naquadah available anymore - We are dead!
	if(Naquadah == 0) then
		self.Entity:SetMaterial("materials/models/reactor-skin-off");
		self.depleted = true;
		self.enabled = false;
		self:SetOverlayText("Naquadah Generator\nDepleted");
	end
	-- Energy conversion when availeble storage
	if(self.enabled and energy < nw_capacity) then
		local rate = (my_capacity+nw_capacity)/2; 
		rate = math.Clamp(rate,0,Naquadah);
		rate = math.Clamp(rate,0,nw_capacity-energy);
		self:SupplyResource("energy",rate);
		self:ConsumeResource("Naquadah",rate);
	end
	if(self.depleted and Dev_Unlink_All) then
		Dev_Unlink_All(self.Entity);
		self:SetWire("Active",-1);
	else
		self:ShowOutput(percent);
		self:SetWire("Active",self.enabled);
		self:SetWire("Energy",math.floor(energy));
		self:SetWire("Naquadah",math.floor(Naquadah));
		
	end
	self.Entity:NextThink(CurTime()+0.5);
	return true;

	

end


--############### Adds Explosions under different circumstances

function ENT:OnTakeDamage(damageInfo)
    if self:GetNWInt("State", 1) == 4 then return end
	self.Entity:SetHealth(self.Entity:Health() - damageInfo:GetDamage())
       
    if(self.Entity:Health() <= 0) then
        self:Destruct()
    end
end

function ENT:Destruct()
	self:SetNWInt("State", 4)
	
	self.Entity:Remove()
	destructEffect = EffectData()
	destructEffect:SetOrigin(self.Entity:GetPos())
	destructEffect:SetScale(50000)
	destructEffect:SetMagnitude(50000)
	util.Effect("Explosion", destructEffect, true, true)
	
	local bombOwner = self.Entity:GetVar("Owner", self.Entity)
	local blastRadius = 5000000
	local blastDamage = 200
	util.BlastDamage(self.Entity, bombOwner, self.Entity:GetPos(), blastRadius, blastDamage)   


if( Naquadah >= 5000000)

then

	self.Entity:Remove()
	destructEffect = EffectData()
	destructEffect:SetOrigin(self.Entity:GetPos())
	destructEffect:SetScale(50000)
	destructEffect:SetMagnitude(50000)
	util.Effect("Explosion", destructEffect, true, true)
	
	local bombOwner = self.Entity:GetVar("Owner", self.Entity)
	local blastRadius = 5000000
	local blastDamage =  200
	util.BlastDamage(self.Entity, bombOwner, self.Entity:GetPos(), blastRadius, blastDamage)
	end
end